package com.zp.crm.Controller.MarketingController;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.google.gson.Gson;
import com.zp.crm.entity.Marketing.MarketingEntity;
import com.zp.crm.service.MarketingService.MarketingService;

@Controller
@RequestMapping("/Marketing")
public class MarketingController {
	
	@Resource
	public MarketingService marService;
	
	@RequestMapping(value="/MarSelectPage",method=RequestMethod.POST)
	public void MarCreateSelectListPage(HttpServletRequest request,HttpServletResponse response, Model model){
		response.setContentType("text/html;charset=utf-8");
		String pagestr=request.getParameter("page");
		int page=1;int size=10;
		if(pagestr != null && pagestr.length() != 0){
			page=Integer.parseInt(pagestr);
		}
		Map<String, Object> map=marService.selectPageSizeList(page,size);
		String text="";
		if((boolean) map.get("success")){
			text=(String)map.get("message");
		}
		try {
			PrintWriter out=response.getWriter();
			out.println(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	@RequestMapping(value="/MarDeleteCode",method=RequestMethod.POST)
	public void MarDeleteCode(HttpServletRequest request,HttpServletResponse response,Model model){
		response.setContentType("text/html;charset=utf-8");
		Map<String, Object> map=new HashMap<>();
		String code=request.getParameter("code");
		if(code != null &&code.length() != 0){
			
			map=marService.deleteByKeyCode(code);
			
		}else{
			map.put("success", false);
			map.put("message", "请操作删除按钮！");
		}
		try {
			PrintWriter out =response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@RequestMapping(value="/MarSelectCreatePage",method=RequestMethod.POST)
	public void MarSelectCreatePage(HttpServletRequest request,HttpServletResponse response,Model model){
		response.setContentType("text/html;charset=utf-8");
		String name=request.getParameter("name");
		String mar=request.getParameter("mar");
		String phoneName=request.getParameter("phoneName");
		String pageStr=request.getParameter("page");
		int page=1;int size=10;
		if(pageStr != null && pageStr.length() !=0){
			page=Integer.parseInt(pageStr);
		}
		Map<String, Object> map=marService.SelectpageCreate(name,mar,phoneName,page,size);
		try {
			PrintWriter out = response.getWriter();
			String text=(String)map.get("message");
			out.println(text);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping(value="/MarEditSelect",method=RequestMethod.POST)
	public void MarEditSelect(HttpServletRequest request,HttpServletResponse response,Model model){
		response.setContentType("text/html;charset=utf-8");
		Map<String, Object> map=new HashMap<String, Object>();
		String code = request.getParameter("code");
		String user_id=request.getParameter("un");
		if(code != null && code.length()>0){
			map=marService.selectEditCreateBycode(code,user_id);
		}else{
			map.put("success", false);
			map.put("message", "请按照正常方式进入！");
		}
		try {
			PrintWriter out =response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@RequestMapping(value="/marInsert",method=RequestMethod.POST)
	public void MarInsert(HttpServletRequest request,HttpServletResponse response,Model model){
		response.setContentType("text/html;charset=utf-8");
		String code = request.getParameter("code");
		String sourceCode = request.getParameter("sourceCode");
		String marName = request.getParameter("marName");
		String marIntStr = request.getParameter("marInt");
		int marInt=Integer.parseInt(marIntStr);
		String marmak = request.getParameter("marmak");
		String marPhoneName = request.getParameter("marPhoneName");
		String marPhone = request.getParameter("marPhone");
		String marms = request.getParameter("marms");
		String marCreateUser = request.getParameter("marCreateUser");
		MarketingEntity entity=new MarketingEntity(code, sourceCode, marName, marInt, marmak, marPhoneName, marPhone, marms, marCreateUser, new Date());
		Map<String, Object> map=marService.MarInsert(entity);
		try {
			PrintWriter out = response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 @RequestMapping(value="/marEdit",method=RequestMethod.POST)
	public void MarEdit(HttpServletRequest request,HttpServletResponse response,Model model){
			response.setContentType("text/html;charset=utf-8");
			String code = request.getParameter("code");
			String sourceCode = request.getParameter("sourceCode");
			String marName = request.getParameter("marName");
			String marIntStr = request.getParameter("marInt");
			int marInt=Integer.parseInt(marIntStr);
			String marmak = request.getParameter("marmak");
			String marPhoneName = request.getParameter("marPhoneName");
			String marPhone = request.getParameter("marPhone");
			String marms = request.getParameter("marms");
			String marCreateUser = request.getParameter("marCreateUser");
			MarketingEntity entity=new MarketingEntity(code, sourceCode, marName, marInt, marmak, marPhoneName, marPhone, marms, marCreateUser, new Date());
			Map<String, Object> map=marService.MarEdit(entity);
			try {
				PrintWriter out = response.getWriter();
				String json=new Gson().toJson(map);
				out.println(json);
			} catch (IOException e) {
				e.printStackTrace();
			}
	  }
	@RequestMapping(value="/zdendit",method=RequestMethod.POST)
	public void MarZdEdit(HttpServletResponse response,HttpServletRequest request,Model model){
			response.setContentType("text/html;charset=utf-8");
			String code = request.getParameter("code");
			String name = request.getParameter("name");
			System.out.println(name);
			String stuts="已指派";
			Map<String, Object> map=marService.MarZdEdit(code,name,stuts);
			try {
				PrintWriter out = response.getWriter();
				String json=new Gson().toJson(map);
				out.println(json);
			} catch (IOException e) {
				e.printStackTrace();
			}
	  }
	@RequestMapping(value="/LoadUserMarList",method=RequestMethod.POST)
	public void LoadUserMarList(HttpServletResponse response,HttpServletRequest request,Model model){
		response.setContentType("text/html;charset=utf-8");
		String user_id=request.getParameter("user_id");
		String pagestr=request.getParameter("page");
		int page=1;int size=10;
		if(pagestr != null && !pagestr.equals("0")){
			page=Integer.parseInt(pagestr);
		}
		Map<String, Object> map=new HashMap<String, Object>();
		if(user_id != null){
			map=marService.LoadUserMarList(user_id,page,size);
		}else{
			map.put("success", false);
		}
		String a="";
		if((boolean) map.get("success")){
			a=(String)map.get("message");
		}else{
			a="系统错误";
		}
		try {
			PrintWriter out =response.getWriter();
			out.println(a);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@RequestMapping(value="LoadUserSelectCreateMarList",method=RequestMethod.POST)
	public void LoadUserSelectCreateMarList(HttpServletResponse response,HttpServletRequest request,Model model){
		response.setContentType("text/html;charset=utf-8");
		String user_id=request.getParameter("user_id");
		String pageStr=request.getParameter("page");
		String name=request.getParameter("name");
		String mar=request.getParameter("mar");
		String phoneName=request.getParameter("phoneName");
		int page=1;int size=10;
		if(pageStr != null && pageStr.length() !=0){
			page=Integer.parseInt(pageStr);
		}
		Map<String, Object> map=marService.LoadUserSelectCreateMarList(user_id,name,mar,phoneName,page,size);
		try {
			PrintWriter out = response.getWriter();
			String text=(String)map.get("message");
			out.println(text);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping(value="/LoadUserUpStu",method=RequestMethod.POST)
	public void LoadUserUpdateStuts(HttpServletRequest request,HttpServletResponse response,Model model){
		response.setContentType("text/html;charset=utf-8");
		String code=request.getParameter("code");
		String stuts=request.getParameter("stuts");
		Map<String, Object> map=marService.DevelopUpStuts(code,stuts);
		try {
			PrintWriter out = response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
